﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DICE_GS;

public class AddSocialMenuItems : MonoBehaviour {
    public FriendListItem friendUnitPrefab;
    public FriendListItem friendRequestedUnitPrefab;
    private ArrayList unitList = new ArrayList();
        
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    public void RefreshItems()
    {

    }

	public void ClearItems()
	{
		foreach (GameObject unit in unitList)
		{
			Transform trans = unit.transform;
			trans.transform.localPosition = new Vector3(0f, 1500f, 0f);
			trans.transform.parent = null;
			DestroyImmediate(trans.gameObject);
		}
		
		unitList.Clear();
	}

    public void SetItems(SOCIALMENUTYPE type)
    {
        foreach (GameObject unit in unitList)
        {
            Transform trans = unit.transform;
            trans.transform.localPosition = new Vector3(0f, 1500f, 0f);
            trans.transform.parent = null;
            DestroyImmediate(trans.gameObject);
        }

        unitList.Clear();

        transform.parent.localPosition = Vector3.zero;
        Vector2 range = transform.parent.GetComponent<UIPanel>().clipOffset;
        range.x = 0f;
		transform.parent.GetComponent<UIPanel>().clipOffset = range;

        switch (type)
        {
            case SOCIALMENUTYPE.FRIENDLIST:
                for (int i = 0; i < MyInfo.Instance.MyFriendList.Count; i++)
                {
//                    FriendListItem item = Instantiate(friendUnitPrefab, new Vector3(0f, 0f, -0.01f), Quaternion.identity) as FriendListItem;
					FriendListItem item = LTPrefabManager.InstantiateC(PrefabTypes.Panel_Button_mail, Vector3.zero, Quaternion.identity) as FriendListItem;
                    item.transform.parent = transform;
                    item.transform.localScale = Vector3.one;
                    item.index = i;
                    item.manager = this;
                    item.friendLevel.text = "LV. "+ MyInfo.Instance.MyFriendList[i].friendLevel.ToString();
                    item.friendName.text = MyInfo.Instance.MyFriendList[i].friendName.ToString();
//                    item.friendLastConnectionTime.text = "1분전";
                    //CUnitData unitData = CUnitDataManager.Instance.GetUnitData(MyInfo.Instance.MyFriendList[i].leaderUnitClassId);
					item.friendUnitImage.spriteName = "Char0"+MyInfo.Instance.MyFriendList[i].leaderUnitClassId;
                    unitList.Add(item.gameObject);
					item.acceptButtonObject.SetActive(false);
					item.deleteButtonObject.SetActive(false);
					item.mailTypeLabel.gameObject.SetActive(false);
                }
                break;
            case SOCIALMENUTYPE.ADDFRIEND:
                break;
            case SOCIALMENUTYPE.RECOMMANDFRIEND:
            case SOCIALMENUTYPE.MAILBOX:
				int indexCount = 0;
                for (int i = 0; i < MyInfo.Instance.MyFriendRequestedList.Count; i++)
                {
//                    FriendListItem item = Instantiate(friendRequestedUnitPrefab, new Vector3(0f, 0f, -0.01f), Quaternion.identity) as FriendListItem;
					FriendListItem item = LTPrefabManager.InstantiateC(PrefabTypes.Panel_Button_mail, Vector3.zero, Quaternion.identity) as FriendListItem;
                    item.transform.parent = transform;
                    item.transform.localScale = Vector3.one;
                    item.index = indexCount++;
                    item.manager = this;
                    item.friendLevel.text = "LV. " + MyInfo.Instance.MyFriendRequestedList[i].friendLevel.ToString();
                    item.friendName.text = MyInfo.Instance.MyFriendRequestedList[i].friendName.ToString();
//                    item.friendLastConnectionTime.text = "1분전";
					item.deleteButtonObject.GetComponent<UIButtonMessage>().functionName = "DenyFriendRequest";
//					item.deleteButtonObject.GetComponent<UISprite>().spriteName = "button_refusal";
                    //CUnitData unitData = CUnitDataManager.Instance.GetUnitData(MyInfo.Instance.MyFriendRequestedList[i].leaderUnitClassId);
					item.friendUnitImage.spriteName = "Char0"+MyInfo.Instance.MyFriendRequestedList[i].leaderUnitClassId;
					item.mailTypeLabel.text = CStringTableManager.Instance.GetItemById(1634);
                    unitList.Add(item.gameObject);
                }
			
				for (int i = 0; i < MyInfo.Instance.MyMailboxList.Count; i++)
                {
//                    FriendListItem item = Instantiate(friendUnitPrefab, new Vector3(0f, 0f, -0.01f), Quaternion.identity) as FriendListItem;
					FriendListItem item = LTPrefabManager.InstantiateC(PrefabTypes.Panel_Button_mail, Vector3.zero, Quaternion.identity) as FriendListItem;
                    item.transform.parent = transform;
                    item.transform.localScale = Vector3.one;
                    item.index = indexCount++;
                    item.manager = this;
                    item.friendLevel.text = MyInfo.Instance.MyMailboxList[i].name;
					Debug.Log(MyInfo.Instance.MyMailboxList[i].title);
                    item.friendName.text = MyInfo.Instance.MyMailboxList[i].title;
//                    item.friendLastConnectionTime.text = "1분전";
					item.acceptButtonObject.GetComponent<UIButtonMessage>().functionName = "AcceptMail";
					item.deleteButtonObject.GetComponent<UIButtonMessage>().functionName = "DeleteMail";
					if( MyInfo.Instance.MyMailboxList[i].attachType != 0 )
						item.acceptButtonObject.SetActive(true);
					else
						item.acceptButtonObject.SetActive(false);
//                    CUnitData unitData = CUnitDataManager.Instance.GetUnitData(MyInfo.Instance.MyFriendList[i].leaderUnitClassId);
//                    if( unitData != null )
//                        item.friendUnitImage.spriteName = unitData.name;
                    unitList.Add(item.gameObject);
                }
                break;
            case SOCIALMENUTYPE.GIFT:
                break;
        }

        GetComponent<UIGrid>().Reposition();
    }
}
